package vader.config.sms.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teautil.Common;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;
import vader.config.sms.SendSmsService;
import vader.config.sms.config.AliyunSmsConfig;

/**
 * @author yongxin
 * @Title:短信发送
 * @Package
 * @Description:短信发送统一处理
 * @date 2021/3/1515:30
 * pom.xml
 */
@Slf4j
@Service
@ConditionalOnProperty(name = "fancy.sms.enable", havingValue = "true")
public class SendSmsServerimpl implements SendSmsService {
    //定时任务失败提醒
    private static final String SCHEDULED_TASK_FAIL = "SMS_230960382";

    @Autowired
    private AliyunSmsConfig smsConfig;

    @Value("${spring.profiles.active}")
    private String env = "test";

    private static final String prod = "prod";
    private static final String pre = "pre";
    private static final String operator_signName = "Fancy翻牌";


    @Override
    public void sendMessageToOperatorPerson(String mobile, String name, String message) {
        String signName = operator_signName;

        try {
            if ("prod".equalsIgnoreCase(prod)) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("name", name);
                jsonObject.put("message", message);
                SendSmsRequest sendSmsRequest = new SendSmsRequest()
                        .setPhoneNumbers(mobile)
                        .setSignName(signName)
                        .setTemplateCode(SCHEDULED_TASK_FAIL)
                        .setTemplateParam(JSON.toJSONString(jsonObject));
                SendSmsResponse sendSmsResponse = smsConfig.getSmsClient().sendSms(sendSmsRequest);
                String code = sendSmsResponse.body.code;
                if (!Common.equalString(code, "OK")) {
                    log.error("not ok group operator reminder sms send fail PhoneNumbers={},message={}", mobile, JSONObject.toJSONString(sendSmsResponse));
                }
            }
        } catch (Exception e) {
            log.error("group operator reminder sms send exception failPhoneNumbers:{},message:{}", mobile, SCHEDULED_TASK_FAIL);
            throw new RuntimeException(e);
        }
    }
}
